package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.entity.ApplicationFormControl;
import com.evil.common.application.dto.form.control.ControlBaseDTO;
import com.evil.common.application.util.ApplicationRedisKeyUtil;
import com.evil.common.core.constant.RedisConstant;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.core.util.CacheMapperUtil;
import com.evil.common.core.util.StreamUtil;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 应用表单控件表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface ApplicationFormControlMapper extends BaseMapper<ApplicationFormControl> {

    Optional<ApplicationFormControl> isExistById(Long id);

    default Optional<ApplicationFormControl> isExistById_Cache(Long id) {
        return CacheMapperUtil.cacheById(
                id,
                ApplicationFormControl.class,
                this::findById,
                ApplicationRedisKeyUtil::requestUuidControlKey,
                RedisConstant.REQUEST_UUID_EXPIRE_IN_SECOND
        );
    }

    default ApplicationFormControl findById(Long id) {
        return this.isExistById(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_CONTROL_NOT_EXIST));
    }

    default ApplicationFormControl findById_Cache(Long id) {
        return this.isExistById_Cache(id).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_FORM_CONTROL_NOT_EXIST));
    }

    List<ApplicationFormControl> findByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    default List<ApplicationFormControl> findByIds_Cache(Collection<Long> ids, boolean filterDeleted) {
        return CacheMapperUtil.cacheByIds(
                ids,
                filterDeleted,
                ApplicationFormControl.class,
                this::findByIds,
                ApplicationFormControl::getFormId,
                ApplicationFormControl::getIsDeleted,
                null,
                ApplicationRedisKeyUtil::requestUuidControlKeyMap,
                RedisConstant.REQUEST_UUID_EXPIRE_IN_SECOND
        );
    }

    List<ApplicationFormControl> findByFormId(@Param("formId") Long formId);

    List<ApplicationFormControl> findByFormIds(@Param("formIds") Collection<Long> formIds);

    List<ApplicationFormControl> findByFormIdAndControlCode(@Param("formId") Long formId,
                                                            @Param("controlCode") String controlCode);

    default Map<Long, ApplicationFormControl> findMapByIds(Collection<Long> ids, boolean filterDeleted) {
        return StreamUtil.toMapK(this.findByIds(ids, filterDeleted), ApplicationFormControl::getControlId);
    }

    ControlBaseDTO findBaseById(@Param("id") Long id);

    List<ControlBaseDTO> findBaseByIds(@Param("ids") Collection<Long> ids, @Param("filterDeleted") boolean filterDeleted);

    List<ControlBaseDTO> findBaseByFormId(@Param("formId") Long formId);
}